package com.seeyon.apps.portalIntegrate.sso;

import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.portal.sso.SSOLoginHandshakeAbstract;
import java.util.Base64;
import java.util.Base64.Decoder;
import org.apache.commons.logging.Log;

public class PISSOLoginHandshakeImpl extends SSOLoginHandshakeAbstract
{
  private OrgManager orgManager;
  private static Log log = CtpLogFactory.getLog(PISSOLoginHandshakeImpl.class);

  public String handshake(String ticket) {
    if (this.orgManager == null) {
      this.orgManager = ((OrgManager)AppContext.getBean("orgManager"));
    }

    String encodeString = new String(Base64.getDecoder().decode(ticket.getBytes()));
    char[] charArray = encodeString.toCharArray();
    for (int i = 0; i < charArray.length; ++i)
    {
      int tmp51_49 = i;
      char[] tmp51_48 = charArray; tmp51_48[tmp51_49] = (char)(tmp51_48[tmp51_49] - '\1');
    }
    String loginName = new String(charArray);
    log.info("单点登录登录名：" + loginName);
    try {
      V3xOrgMember member = this.orgManager.getMemberByLoginName(loginName);
      if (member != null) 
      return loginName;
    }
    catch (BusinessException e) {
      log.error(e.getMessage(), e);
    }
    return null;
  }

  public void logoutNotify(String ticket)
  {
  }
}